// 优化前
#include <unordered_map>
#include <unordered_set>
using namespace std;
class Solution
{
public:
    bool closeStrings(string word1, string word2)
    {
        if (word1.size() != word2.size())
            return false;
        // 种类一样 并且个数一样
        unordered_map<char, int> u_maps[2];
        for (char c : word1)
            u_maps[0][c]++;
        for (char c : word2)
            u_maps[1][c]++;
        // 种类数量不一样
        if (u_maps[0].size() != u_maps[1].size())
            return false;
        for (auto pair : u_maps[0])
        {
            if (!u_maps[1][pair.first])
                return false;
        }
        unordered_multiset<int> u_sets[2];
        for (char c : word1)
            u_sets[0].insert(u_maps[0][c]);
        for (char c : word2)
            u_sets[1].insert(u_maps[1][c]);
        return u_sets[0] == u_sets[1];
    }
};
// 优化后
#include <unordered_map>
#include <unordered_set>
#include <stdlib.h>
using namespace std;
class Solution
{
public:
    bool closeStrings(string word1, string word2)
    {
        if (word1.size() != word2.size())
            return false;
        // 种类一样 并且个数一样
        short u_maps[2][26];
        memset(u_maps, 0, 26 * 2 * sizeof(short));
        for (char c : word1)
            u_maps[0][c - 'a']++;
        for (char c : word2)
            u_maps[1][c - 'a']++;
        // 种类不一样
        for (int i = 0; i < 26; i++)
        {
            if ((u_maps[0][i] && !u_maps[1][i]) || (!u_maps[0][i] && u_maps[1][i]))
            {
                return false;
            }
        }
        sort(u_maps[0], u_maps[0] + 26, greater());
        sort(u_maps[1], u_maps[1] + 26, greater());
        for (int i = 0; i < 26; i++)
        {
            if (u_maps[0][i] != u_maps[1][i])
                return false;
        }
        return true;
    }
};